package com.wish.g01.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g01.base.dto.FactorAppDto;
import com.wish.g01.base.entity.CfContract;
import com.wish.g01.base.dto.CwReceiptPlanDto;

import com.wish.g01.base.entity.CwReceiptPlan;
import com.wish.g01.base.entity.JrFactoringDetail;
import com.wish.g01.base.entity.JrProduct;
import com.wish.g01.base.mapper.CwReceiptPlanMapper;
import com.wish.g01.base.service.ICfContractService;
import com.wish.g01.base.service.ICwReceiptPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.g01.base.service.IJrProductService;
import com.wish.g01.base.vo.FactoringDeatilVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import java.util.List;

/**
 * <p>
 * 收款计划表 服务实现类
 * </p>
 *
 * @author hao
 * @since 2022-05-24
 */
@Service
public class CwReceiptPlanServiceImpl extends ServiceImpl<CwReceiptPlanMapper, CwReceiptPlan> implements ICwReceiptPlanService {

    @Autowired
    private ICfContractService iCfContractService;

    @Autowired
    private IJrProductService iJrProductService;

    @Override
    public FactoringDeatilVo factoringApply(FactorAppDto factorAppDto) {

        FactoringDeatilVo factoringDeatilVo = new FactoringDeatilVo();
        //           合同
        CfContract cfContract = iCfContractService.getById(factorAppDto.getContractId());

        BeanUtils.copyProperties(cfContract, factoringDeatilVo);

        factoringDeatilVo.setCfContractId(new Long(factorAppDto.getContractId()));

//        通过合同的id,查询收款计划表
        QueryWrapper<CwReceiptPlan> cwReceiptPlanQueryWrapper = new QueryWrapper<>();
        cwReceiptPlanQueryWrapper.eq("contract_id", factorAppDto.getContractId());

//        出房
        cwReceiptPlanQueryWrapper.eq("contract_type", 2);
//        未付款
        cwReceiptPlanQueryWrapper.eq("sk_status", 1);
//         预定收款日期
//        cwReceiptPlanQueryWrapper.ge("collection_date", LocalDateTime.now());


        List<CwReceiptPlan> cwReceiptPlans = getBaseMapper().selectList(cwReceiptPlanQueryWrapper);

        //System.out.println("44444444444444444444444444444440000" + cwReceiptPlans.size());
        if (cwReceiptPlans != null && cwReceiptPlans.size() > 0) {


//        借款账单开始时间
            factoringDeatilVo.setBorrowedStart(cwReceiptPlans.get(0).getPaymentDaysBegin());
//           借款账单截止时间
            factoringDeatilVo.setBorrowedEnd((cwReceiptPlans.get(cwReceiptPlans.size() - 1).getPaymentDaysEnd()));

//          应收租约金额：即剩余租约金房租合计
//        应收租金总计
            BigDecimal shouldMonthRent = BigDecimal.ZERO;
            for (CwReceiptPlan cwReceiptPlan : cwReceiptPlans) {
                shouldMonthRent = shouldMonthRent.add(cwReceiptPlan.getPaymentAmount());
            }
            factoringDeatilVo.setShouldMonthRent(shouldMonthRent);

            // 借款周期(多少个月 = 付几 * 剩余未付房租账期数)
            factoringDeatilVo.setFactoringPeriod(cfContract.getPaymentType() * cwReceiptPlans.size());

//            System.out.println("666666666666666666666666666666666666666666666666660" + cfContract.getPaymentType() * cwReceiptPlans.size());

            JrProduct product = iJrProductService.getById(factorAppDto.getProductId());
            // 保理授信金额总金额 = 应收租约金额*(授信额度/100) (备注：四舍五入)
//			                         授信额度:指产品的授信额度

            factoringDeatilVo.setFactoringCreditMoney(shouldMonthRent.multiply(product.getCreditLine().divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)).setScale(2, BigDecimal.ROUND_HALF_UP));

//            System.out.println("777777777777777777777777" + factoringDeatilVo.getFactoringCreditMoney());
            // 利息 = 保理授信总金额 *月利率 *月数  = 保理授信总金额 * 年利率/12 *月数 (备注：四舍五入)
            factoringDeatilVo.setFactoringInterest(factoringDeatilVo.getFactoringCreditMoney().multiply(new BigDecimal(factorAppDto.getInterestRate()/(100 * 12))).multiply(new BigDecimal(factoringDeatilVo.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));

            // 服务费 = 保理授信金额*服务费率/12*月数(备注：四舍五入)
            factoringDeatilVo.setServiceFee(factoringDeatilVo.getFactoringCreditMoney().multiply(new BigDecimal(factorAppDto.getServiceFee()/(100 * 12))).multiply(new BigDecimal(factoringDeatilVo.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));
//            System.out.println("9999999999999999999999");
//            factoringDeatilVo.setFactoringInterest(new BigDecimal(78));
//            factoringDeatilVo.setServiceFee(new BigDecimal(88));

        }
        return factoringDeatilVo;
    }

    @Override
    public List<CwReceiptPlan> sumList(CwReceiptPlanDto dto) {
        return getBaseMapper().sumList(dto);
    }

    @Override
    public List<CwReceiptPlan> listShow(CwReceiptPlanDto vo) {
        return getBaseMapper().listShow(vo);

    }
}

